import { login, roterList, getRoutes,formatRouter} from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import Layout from '@/layout'



import {
  Message
} from 'element-ui'
const user = {
  namespaced: true,
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    routes: [],
    addRoutes: [],
    permissionList: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_PERMISSIONLIST: (state, permissionList) => {
      state.permissionList = permissionList
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },

     SET_ROUTERS: (state,routers) =>{
    state.routers = routers
  }
  },

  actions: {
  // user login
    login({ commit }, userInfo) {
      userInfo.username = userInfo.username.trim()

      return new Promise((resolve, reject) => {
        login(userInfo).then(res => {
          console.log(res)
          commit('SET_TOKEN', res.data.bearer_token)
          commit('SET_NAME', res.data.adminInfo.username)
          commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
          // 通过auth设置vue_admin_template_token
          setToken(res.data.bearer_token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // getRouter({commit,state}){
    //   console.log(state)
    //   return new Promise((resolve,reject)=>{

    //       roterList(state.token).then(response =>{
    //         console.log("权限表",response)
    //       }).catch(error=>{
    //         reject(error)
    //       })
    //   })
    // },
   getRoute({ commit, state }, constRoutes) {
      return new Promise((resolve, reject) => {
		  console.log(state.token)
        getRoutes(state.token).then(res => {
			
			 const { data } = res
			  console.log('res',data)
          let formatRouters= formatRouter(data)
		  console.log('formatRouters',formatRouters)
		  let errorRouter = {
		    path:"*",
		    redirect: '/404',
		    hidden:'true',
		    name:"404"
		  }
		  formatRouters.push(errorRouter);
		  if (!formatRouters || formatRouters.length <= 0) {
		    reject('当前用户没有配置权限信息')
		  }
		  commit('SET_ROUTERS', formatRouters)
          resolve(res.data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // get user info
    // getInfo({ commit, state }) {
    //     return new Promise((resolve, reject) => {
    //       getInfo(state.token).then(response => {
    //         const data = response
    //         // roles must be a non-empty array
    //         console.log(response)
    //         if (data.data!=null) {
    //           commit('SET_ROLES', data.data.roleName)
    //           Cookies.set('role',data.data.roleName)
    //         }else{
    //           this.logout()
    //         }

    //         resolve(data)
    //       }).catch(error => {
    //         reject(error)
    //       })
    //     })
    //   },

    // user logout
    // 接口版注销登录：user logout
    // logout({ commit, state }) {
    //   return new Promise((resolve, reject) => {
    //     logout(state.token).then(() => {
    //       removeToken() // must remove  token  first
    //       resetRouter()
    //       commit('RESET_STATE')
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 注销登录：清除token，remove token
    resetToken({
      commit
    }) {
      return new Promise(resolve => {
        removeToken()
		 resetRouter()// must remove  token  first
        resolve()
      })
    }

  // dynamically modify permissions
  }
}

export default user
